<?php
include 'config.php';

session_start();

if (!session_is_registered(myusername))
{
        header("Location: index.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html>
        <head>
        <title>PHP Occam Interface</title>
        <script type="text/javascript">

	function commandType(type) {
	
		if (type == "suspend_restore") {
			/*var html="";
			html=html+"<input type=\"checkbox\" name=\"cmd\" value=\"suspend_internet\"></input>Suspend Internet<br>\n";
			html=html+"<input type=\"checkbox\" name=\"cmd\" value=\"suspend_video\"></input>Suspend Video<br>\n";
			html=html+"<input type=\"checkbox\" name=\"cmd\" value=\"restore_internet\"></input>Restore Internet<br>\n";
			html=html+"<input type=\"checkbox\" name=\"cmd\" value=\"restore_video\"></input>Restore Video<br>\n";*/
			var html="<select class=\"input\" id=\"cmd\" name=\"cmd\">\n";
			html=html+"<option value=\"suspend_internet\">Suspend Internet</option>\n";
			html=html+"<option value=\"suspend_video\">Suspend Video</option>\n";
			html=html+"<option value=\"restore_internet\">Restore Internet</option>\n";
			html=html+"<option value=\"restore_video\">Restore Video</option>\n";
			html=html+"</select>";
		}
		else if (type == "speed_change") {
			var html="<select class=\"input\" id=\"cmd\" name=\"cmd\">\n";
			html=html+"<option value=\"\">Select Command...</option>\n";
			html=html+"<option value=\"standard_bronze\">Bronze Speed 1024kb/768kb</option>\n";
			html=html+"</select>";
		}
		else if (type == "show_dhcp_clients") {
			var html="<select class=\"input\" id=\"cmd\" name=\"cmd\">\n";
			html=html+"<option value=\"\">Select Command...</option>\n";
			html=html+"<option value=\"show_dhcp_clients\">Show DHCP Clients</option\n";
			html=html+"</select>";
		}
		else {
			var html="<select class=\"input\" >";
			html=html+"</select>";
		}

		document.getElementById("cmd_container").innerHTML=html;
	}

        </script>
        </head>
        <body onLoad="commandType('default')">
	        <form id="subAddForm" action="" method="GET">
                <table>
                        <tr><td>Blade:</td><td><select class="input" id="commandIP" name="ip">
				<?php
					$result = mysql_query("SELECT * FROM devices WHERE device_type = '6312'");
					
					while ( $row = mysql_fetch_array($result))
					{
						$ip_address = $row['device_ip'];
						$hostname = $row['device_name'];
						$device_type = $row['device_type'];

						$port_result = mysql_query("SELECT * FROM device_types WHERE device_type_id = '$device_type'");
						$port_count = mysql_fetch_array($port_result);
						$port_count = $port_count['device_dsl_ports'];						

						//TODO:  Show hostname or IP based on users option in profile
						if ($_SESSION['host_or_ip'] == "ip") {
							echo "<option value=\"$ip_address\">$ip_address</option>\n";
						}
						else {
							echo "<option value=\"$ip_address\">$hostname</option>\n";
						}
					}
				?>
			</select></td></tr>
			<tr><td>Port:</td><td id="ports"><select class="input" id="commandPort" name="port">
				<?php
					//TODO:  Have port total grabbed from DB based on device type.
					$port = 1;
					$port_total = 22;				
					
					echo "<option value=\"\"></option>\n";
					while ( $port <= $port_total )
					{
						//  Do not allow ports 5 or 6 to be changed!!  They are used for interconnect.
						if ($port != 5 AND $port != 6) {
							echo "<option value=\"$port\">$port</option>\n";
						}
						$port = $port+1;
					}
				?>
			</select></td></tr>
			</table>
			<div id="subInfoFields" style="padding-top: 10px; display: none">
			<table>
			<tr><td>Subscriber Name &amp; Fiber:</td><td><input class="input" type="text" id="subName"></input>* &amp; will be substituted with "and" for the time being</td></tr>
                        <tr><td>Circuit-id:</td><td><input class="input" type="text" id="subPhone" maxlength="8"></input></td></tr>
			<tr><td>IP Address: </td><td><input class="input" type="text" id="subOntIP" maxlength="15" /><input type="button" id="testButton" value="Get IP!"/>&#160;&lt;-- Click on "Get IP!" to fill in the IP Address field to the left</td></tr>
			<!--<tr><td>Subscriber Login:</td><td><input class="input" type="text" id="subLoginUsername"></input>Password: <input class="input" type="text" id="subLoginPassword"></input></td></tr>-->
			<tr><td>Internet Speed: </td><td><select class="input" id="subInternetSpeed">
					<option value="standard_bronze">Bronze Speed 7Mb/768kb</option>
				</select></td><td></td></tr>
                        <tr><td>Has Video?: </td><td><input type="checkbox" id="subVideoService" value="True"></input></td></tr>
			<tr><td></td><td><input type="button" id="commandSubmit" value="Send!" onClick="MochaUI.notification('Please Wait...')"/><input type="reset" value="cancel"/><input type="checkbox" id="commandDebug" name="debug" value="True" title="Display verbose output"></input></td></tr>

                	</table>
			</div>
		</form>
<script type="text/javascript">

        $('commandSubmit').addEvent('click', function(e){
                var url = 'pages/blade_info.php?';
		url=url+'ip='+$('commandIP').value;
		url=url+'&port='+$('commandPort').value;
		url=url+'&subName='+$('subName').value.replace(/&/, "and");
		url=url+'&subPhone='+$('subPhone').value.replace(/&/, "");
		url=url+'&subOntIP='+$('subOntIP').value.replace(/&/, "");
		url=url+'&subInternetSpeed='+$('subInternetSpeed').value;
		if ($('subVideoService').checked) {
			url=url+'&subVideoService='+$('subVideoService').value;
		}
		else {
			url=url+'&subVideoService=False';
		}
		url=url+'&cmd=occam_fiber_add_customer';
		if ($('commandDebug').checked) {
			url=url+'&debug='+$('commandDebug').value;
		}
		url=url+'&nocache='+Math.random();
		MochaUI.updateContent({
                        'element': $('panel1'),
                        'loadMethod': 'xhr',
                        'url': url,
                        'title': 'Results',
                        'padding': { top: 8, right: 8, bottom: 8, left: 8 }
                });
        	showSpinner();
	});

	$('commandIP').addEvent('change', function(e){
		if ($('commandPort').value != ""){ 
			$('subInfoFields').setStyle('display', 'block');
			var url = 'pages/blade_info.php?';
			url=url+'ip='+$('commandIP').value;
			url=url+'&port='+$('commandPort').value;
			url=url+'&cmd=ont_status';
			url=url+'&nocache='+Math.random();
			MochaUI.updateContent({
				'element': $('panel2'),
				'loadMethod': 'xhr',
				'url': url,
				'title': 'Port Information',
				'padding': { top: 8, right: 8, bottom: 8, left: 8 }
			});
			showSpinner();
		}
	});

	$('commandPort').addEvent('change', function(e){
		$('subInfoFields').setStyle('display', 'block');
		var url = 'pages/blade_info.php?';
		url=url+'ip='+$('commandIP').value;
		url=url+'&port='+$('commandPort').value;
		url=url+'&cmd=ont_status';
		url=url+'&nocache='+Math.random();
		MochaUI.updateContent({
			'element': $('panel2'),
			'loadMethod': 'xhr',
			'url': url,
			'title': 'Port Information',
			'padding': { top: 8, right: 8, bottom: 8, left: 8 }
		});
		showSpinner();
		
	});

	$('testButton').addEvent('click', function(e){
		var url = 'pages/query.php?';
		url = url+'blade_ip='+$('commandIP').value;
		url = url+'&port='+$('commandPort').value;
		url = url+'&cmd=ontIP';
		url = url+'&nocache='+Math.random();
		var request = new Request({
                        'url': url,
                        'method': 'get',
                        'update': 'refresh_me',
                        onComplete: function(response) {
                                $('subOntIP').set('value',response);
                        }
                }).send();

	});

</script>
	  </body>
</html>
